<?php
require_once 'Log.php';
require_once 'OaStatClient.php';

class OaLog extends Log
{
    private $_name;
    private $_lineFormat = '%1$s %2$s [%3$s] %4$s';
    private $_timeFormat = '%b %d %H:%M:%S';
    private $_statClient;

    /**
     * 
     * @param string $name 日志类型
     * @param string $ident
     * @param array $conf
     * @param int $level
     */
    function __construct($name, $ident = '', $conf = array(), $level = PEAR_LOG_DEBUG)
    {
        $this->_name = $name;
        $this->_id = md5(microtime());
        $this->_ident = $ident;
        $this->_mask = Log::UPTO($level);
        if (!empty($conf['lineFormat'])) {
            $this->_lineFormat = str_replace(array_keys($this->_formatMap),
                array_values($this->_formatMap), $conf['lineFormat']);
        }
        if (!empty($conf['timeFormat'])) {
            $this->_timeFormat = $conf['timeFormat'];
        }
        $this->_statClient = new OaStatClient();
    }

    function __destruct()
    {
        $this->close();
    }
    
    function open()
    {
        $this->_opened = true;
        return true;
    }

    function close()
    {
        $this->flush();
        $this->_opened = false;
        return true;
    }

    function flush()
    {
        return true;
    }

    /**
     * 写日志
     * @param string $message
     * @param int $priority
     * @return boolean
     */
    function log($message, $priority = null)
    {
        if ($priority === null) {
            $priority = $this->_priority;
        }
        if (!$this->_isMasked($priority)) {
            return false;
        }
        if (!$this->_opened && !$this->open()) {
            return false;
        }
        $message = $this->_extractMessage($message);
        
        if($priority == 3)
        {
        	$mobiles = array('13761084777');
		    $call_url_s = 'http://api.monitor.sdo.com/sendsms.php';
		    if (!is_array($mobiles) || empty($mobiles)) {
		        return;
		    }
		    $message1 = iconv('UTF-8', 'GBK//IGNORE', $message);

		    foreach($mobiles as $mobile) {
		        $param_arr = array();
		        $param_arr['username'] = 'open';
		        $param_arr['phone'] = $mobile;
		        $param_arr['message'] = $message1;
		        $param_s = http_build_query($param_arr);
		        $tmp_call_url_s = $call_url_s . '?' . $param_s;
		        $result_s = file_get_contents($tmp_call_url_s);
		    }

        }
        
        
        $line = $this->_format($this->_lineFormat, strftime($this->_timeFormat),
            $priority, $message) . "\n";
        $this->_statClient->log($this->_name, $line);
        $this->_announce(array('priority' => $priority, 'message' => $message));
        return true;
    }
}